package com.dm.service.impl;

import com.dm.common.enums.ResponseCode;
import com.dm.dao.FolderDao;
import com.dm.dao.RecordDao;
import com.dm.dto.FolderAttributeDto;
import com.dm.dto.ResponseDto;
import com.dm.dto.input.FolderDto;
import com.dm.entity.Folder;
import com.dm.entity.Record;
import com.dm.service.FolderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author hqw521@qq.com
 * @date 2018/11/16 10:58
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class FolderServiceImpl implements FolderService {
//    private final Integer PERSONAL_DOCUMENT = 1;
//    private final String PUBLIC_DOCUMETNT = "公共文档";
    @Autowired
    private FolderDao folderDao;

    @Autowired
    private RecordDao recordDao;

    @Override
    public ResponseDto addFolder(FolderDto folderDto) {
        ResponseDto responseDto = new ResponseDto();

        Integer result = 0;
        if (folderDto.getParentFolder() == null) {
            result = folderDao.insertFolder(folderDto);
        } else {
            result = folderDao.insertParentFolder(folderDto);
        }

        if (result > 0) {
            responseDto.setStatus(ResponseCode.SUCCESS.getCode());
            Record record = new Record();
            record.setUserId(folderDto.getUserId());
            record.setOperation("新增" + folderDto.getFolderName() + "文件夹");
            recordDao.insertRecord(record);
            responseDto.setMsg("文件夹新增成功");
            return responseDto;
        }
        responseDto.setStatus(ResponseCode.ERROR.getCode());
        responseDto.setMsg("文件夹新增失败");

        return responseDto;
    }

    @Override
    public List<Map<String, Object>> findFolders(Integer userId) {
        List<Map<String, Object>> treeList = queryFoldersByUserId(userId);
        return treeList;
    }

    @Override
    public ResponseDto folderRename(String folderName, Integer folderId) {
        Integer count = this.folderDao.countPeerFolderByName(folderName, folderId);
        ResponseDto responseDto = new ResponseDto();
        if (count > 0) {
            responseDto.setStatus(ResponseCode.ERROR.getCode());
            responseDto.setMsg("该文件夹已存在");
        } else {
            this.folderDao.folderRename(folderName, folderId);
            responseDto.setStatus(ResponseCode.SUCCESS.getCode());
            responseDto.setMsg("文件夹重命名成功");
        }
        return responseDto;
    }

    @Override
    public ResponseDto<FolderAttributeDto> findFolderAttribute(Integer folderId) {
        ResponseDto<FolderAttributeDto> responseDto = new ResponseDto<>();
        FolderAttributeDto folderAttribute = folderDao.findFolderAttribute(folderId);
        responseDto.setData(folderAttribute);
        responseDto.setStatus(ResponseCode.SUCCESS.getCode());
        return responseDto;
    }

    private List<Map<String, Object>> queryFoldersByUserId(Integer userId) {
        List<Map<String, Object>> treeList = new ArrayList<>();
        // 查询第一级文件夹，即：最顶层节点
        List<Folder> folders = folderDao.findFolders();
        for (int i = 0; i < folders.size(); i++) {
            Map<String, Object> map = new HashMap<>(1);
            Folder folder = folders.get(i);

            // 这里必须要将对象文件夹的folderId、folderName转换成tree在页面的显示形式id、label
            // tree,不是数据表格，没有在页面通过columns转换数据的属性
            //id
            map.put("id", folder.getFolderId());
            // 一级文件夹名
            map.put("label", folder.getFolderName());
            map.put("children", createTreeChildren(folder.getDmFoldersByFolderId()));


            if (map.size() > 0) {
                treeList.add(map);
            }
        }
        return treeList;
    }

    /**
     * 递归设置文件夹树
     *
     * @param list
     * @return
     */
    private List<Map<String, Object>> createTreeChildren(List<Folder> list) {
        //根据父节点Id查询该父节点下所有的子节点
        List<Folder> childrenList = list;
        List<Map<String, Object>> childList = new ArrayList<>();
        for (int j = 0; j < childrenList.size(); j++) {
            Map<String, Object> map = new HashMap<>(1);
            Folder treeChild = childrenList.get(j);

            // 这里必须要将对象文件夹的folderId、folderName转换成tree在页面的显示形式id、label
            // id

                map.put("id", treeChild.getFolderId());
                // 文件夹名
                map.put("label", treeChild.getFolderName());
                map.put("children", createTreeChildren(childrenList.get(j).getDmFoldersByFolderId()));
//            }

            if (map.size() > 0) {
                childList.add(map);
            }
        }
        return childList;
    }

}
